// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import {
  reqGetMessage,
  reqUpdateLockState,
  reqUpdateRunningState,
  reqGetCategoryList,
  reqAddVehicle,
} from './service';
import { history } from 'umi';
export default {
  namespace: 'vehicle',
  state: {
    anyDatas: [],
    newVeDatas: [],
    categoryList: [],
  },
  reducers: {
    setStatus(state, { payload }) {
      return { ...state, payload };
    },
    setnewVeDatas(state, { payload }) {
      return { ...state, newVeDatas: payload };
    },
    setCategoryList(state, { payload }) {
      return { ...state, categoryList: payload };
    },
  },
  effects: {
    *getVmessage({ payload }, { call, put }) {
      const res = yield call(reqGetMessage, payload);
      console.log(res);
      yield put({
        type: 'setnewVeDatas',
        payload: res.data,
      });
    },
    *reqAddVehicle({ payload }, { call, put }) {
      const res = yield call(reqAddVehicle, payload);
      try {
        if (res.code === 0) {
          message.success('添加成功');
          yield put({
            type: 'getVmessage',
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqUpdateLockState({ payload }, { call, put }) {
      const res = yield call(reqUpdateLockState, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'getVmessage',
          });
          message.success('更改成功');
        } else {
          message.info(res.msg);
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqUpdateRunningState({ payload }, { call, put }) {
      const res = yield call(reqUpdateRunningState, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'getVmessage',
          });
          message.success('更改成功');
        } else {
          message.info(res.msg);
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetCategoryList({ payload }, { call, put }) {
      const res = yield call(reqGetCategoryList, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setCategoryList',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
  },
};
